package com.wuwei.tms.workbench.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuwei.tms.exception.TmsException;
import com.wuwei.tms.pojo.Borrow;
import com.wuwei.tms.pojo.History;
import com.wuwei.tms.pojo.User;
import com.wuwei.tms.setting.mapper.UserMapper;
import com.wuwei.tms.utils.MyUtils;
import com.wuwei.tms.utils.result.Result;
import com.wuwei.tms.vo.HistoryExcel;
import com.wuwei.tms.vo.HistoryPrint;
import com.wuwei.tms.workbench.mapper.BorrowMapper;
import com.wuwei.tms.workbench.mapper.HistoryMapper;
import com.wuwei.tms.workbench.service.HistoryService;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * RequestMapping中，visitor是游客页面访问的请求
 */
@RequestMapping("/T01/history")
@RestController
//@CrossOrigin
public class HistoryController {



    @Resource
    private HistoryService historyService;

    /**
     * 找到history表中和该飞机和该员工有关的所有数据
     */
    @GetMapping("list")
    public Result<List<HistoryExcel>> list(@RequestParam Map<String, String> params) {
        LocalDateTime borrowDate1 = MyUtils.myDateFormatt((String)params.get("borrowTime1"));
        LocalDateTime borrowDate2 = MyUtils.myDateFormatt((String)params.get("borrowTime2"));
        LocalDateTime returnDate1 = MyUtils.myDateFormatt((String)params.get("returnTime1"));
        LocalDateTime returnDate2 = MyUtils.myDateFormatt((String)params.get("returnTime2"));
        long borrowById = Long.parseLong((String)params.get("borrowById"));
        long barcode = Long.parseLong((String)params.get("barcode"));
        int currentPage = Integer.parseInt((String)params.get("currentPage"));
        int pageSize = Integer.parseInt((String)params.get("pageSize"));
        String airplaneId = params.get("airplaneId");
        List<HistoryExcel> histories = this.historyService.getList(borrowDate1,borrowDate2, returnDate1,returnDate2, borrowById, barcode, airplaneId,currentPage, pageSize);
        return Result.ok(histories);
    }

    /**
     * 找到history表中和该飞机和该员工有关的所有数据
     */
    @GetMapping("total")
    public Result<Long> total(@RequestParam Map<String, String> params) {
        LocalDateTime borrowDate1 = MyUtils.myDateFormatt((String)params.get("borrowTime1"));
        LocalDateTime borrowDate2 = MyUtils.myDateFormatt((String)params.get("borrowTime2"));
        LocalDateTime returnDate1 = MyUtils.myDateFormatt((String)params.get("returnTime1"));
        LocalDateTime returnDate2 = MyUtils.myDateFormatt((String)params.get("returnTime2"));
        long borrowById = Long.parseLong((String)params.get("borrowById"));
        long barcode = Long.parseLong((String)params.get("barcode"));
        String airplaneId = params.get("airplaneId");
        long count = this.historyService.total(borrowDate1,borrowDate2, returnDate1,returnDate2, borrowById, barcode, airplaneId);
        return Result.ok(count);
    }


    /**
     * 用户查询借还历史记录，这里记录需要查询history和user表，显示的是借用者和归还者的姓名
     * @param params
     * @param response
     */
    @GetMapping("download")
    public void download (@RequestParam Map<String, String> params, HttpServletResponse response){
        LocalDateTime borrowDate1 = MyUtils.myDateFormatt((String)params.get("borrowTime1"));
        LocalDateTime borrowDate2 = MyUtils.myDateFormatt((String)params.get("borrowTime2"));
        LocalDateTime returnDate1 = MyUtils.myDateFormatt((String)params.get("returnTime1"));
        LocalDateTime returnDate2 = MyUtils.myDateFormatt((String)params.get("returnTime2"));
        long borrowById = Long.parseLong((String)params.get("borrowById"));
        long barcode = Long.parseLong((String)params.get("barcode"));
        String airplaneId = params.get("airplaneId");
        List<HistoryExcel> historyList = historyService.download(borrowDate1,borrowDate2, returnDate1,returnDate2, borrowById, barcode, airplaneId);

        //把数据写成excel
        String filename = "history.xlsx";
        EasyExcel.write(filename,HistoryExcel.class).sheet().doWrite(historyList);
        //将该文件返回给前端
        // 设置响应的内容类型为 Excel

        // 设置响应头，告诉浏览器该文件以附件形式下载
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename="+filename);
        try(InputStream inputStream = new FileInputStream(filename);
            OutputStream outputStream = response.getOutputStream()){
            // 将文件的内容以流的形式写入响应对象
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
